package com.efounder.eai.data;

import java.util.*;

import com.core.xml.StubObject;
import com.efounder.eai.*;


/**
 * <p>Title: Java Framework</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author Skyline
 * @version 1.0
 */
//--------------------------------------------------------------------------------------------------
//����:
//���\uFFFD: Skyline(2001.12.29)
//ʵ��: Skyline
//�޸�:
//--------------------------------------------------------------------------------------------------
public class JParamObject  implements java.io.Serializable,KeyValue {
  public static final String DEF_BIZMODEL="DEF_BIZMODEL";
  /**
   *
   * @return JParamObject
   */
//  public jfoundation.dataobject.classes.JParamObject getOLDParamObject() {
//    return jfoundation.dataobject.classes.JParamObject.getParamObject(this);
//  }

  protected transient Object loginStub = null;

  public Object getLoginStub() {
    return loginStub;
  }
  public void setLoginStub(Object loginStub) {
    this.loginStub = loginStub;
  }
  public transient Object httpServletRequest  = null;
  public Object getHttpServletRequest() {
    return httpServletRequest;
  }
  public void setHttpServletRequest(Object httpServletRequest) {
    this.httpServletRequest = httpServletRequest;
  }
  public transient Object httpServletResponse = null;
  public Object getHttpServletResponse() {
    return httpServletResponse;
  }
  public void setHttpServletResponse(Object httpServletResponse) {
    this.httpServletResponse = httpServletResponse;
  }
  /**
   *
   */
  public transient Object httpServlet = null;
  /**
   *
   * @return HttpServlet
   */
  public Object getHttpServlet() {
    return httpServlet;
  }
  /**
   *
   * @param httpServlet HttpServlet
   */
  public void setHttpServlet(Object httpServlet) {
    this.httpServlet = httpServlet;
  }
  /**
   *
   */
  static final long serialVersionUID = 1L;
  /**
   *
   */
  protected transient boolean offline = false;
  /**
   *
   * @return boolean
   */
  public boolean isOffline() {
    return offline;
  }
  /**
   *
   * @param offline boolean
   */
  public void setOffline(boolean offline) {
    this.offline = offline;
  }
  /**
   *
   * @return boolean
   */
  public boolean isSecurity() {
    String Security = (String)this.getValue("security",null);
    if ( Security != null && Security.trim().length() > 0 ) {
      return "1".equals(Security);
    } else {
      return EAI.getSecurity();
    }
  }
  /**
   *
   * @param security boolean
   */
  public void setSecurity(boolean security) {
    this.setValue("security",security?"1":"0");
  }
  /**
   *
   */
  private transient String SSLEAIServer = null;
  /**
   *
   * @param def String
   * @return String
   */
  public String getSSLEAIServer(String def) {
    if ( SSLEAIServer == null ) return def;
    return SSLEAIServer;
  }
  /**
   *
   * @param sslEAIServer String
   */
  public void setSSLEAIServer(String sslEAIServer) {
    SSLEAIServer = sslEAIServer;
  }
  /**
   *
   */
  private transient String SSLEAIPort   = null;
  /**
   *
   * @param def String
   * @return String
   */
  public String getSSLEAIPort(String def) {
    if ( SSLEAIPort == null ) return def;
    return SSLEAIPort;
  }
  /**
   *
   * @param sslEAIPort String
   */
  public void setSSLEAIPort(String sslEAIPort) {
    SSLEAIPort = sslEAIPort;
  }
  /**
   *
   */
  private transient String eaiServer = null;
  /**
   *
   * @param def String
   * @return String
   */
  public String getEAIServer(String def) {
    if ( eaiServer == null || eaiServer.trim().length() == 0 ) return def;
    return eaiServer;
  }
  /**
   *
   */
  protected transient String serviceURL = null;
  /**
   *
   * @return String
   */
  public String getServiceURL() {
    return serviceURL;
  }
  /**
   *
   * @param surl String
   */
  public void setServiceURL(String surl) {
    serviceURL = surl;
  }
  /**
   *
   * @param eaiServer String
   */
  public void setEAIServer(String eaiServer) {
    this.eaiServer = eaiServer;
    /**
     * 如果eaiServer为空或者根据eaiServer没有找到配置信息，连当前登陆的帐套
     */
    if ( eaiServer != null && eaiServer.trim().length() > 0 ) {
      StubObject serverStub = EAIServer.getEAIServer(eaiServer);
      if ( serverStub != null ) {
        // 设置相应的数据库设置
        String dbset = serverStub.getString(EAIServer.SERVER_DBID, null);
        if (dbset != null && dbset.trim().length() > 0)
          this.SetValueByEnvName("DataBaseName", dbset);
        dbset = serverStub.getString(EAIServer.SERVER_ZTID, null);
        if (dbset != null && dbset.trim().length() > 0)
          this.SetValueByEnvName("DBNO", dbset);
      } else this.setDefaultDB();
    } else this.setDefaultDB();
  }

  /**
   * 设置当前登录的存储ID和帐套ID
   */
  private void setDefaultDB(){
      this.eaiServer = null;
      this.SetValueByEnvName(EAIServer.SERVER_DBID, (String)getObject(EAIServer.SERVER_DBID, ""));
      this.SetValueByEnvName(EAIServer.SERVER_ZTID, (String)getObject(EAIServer.SERVER_ZTID, ""));
  }

  /**
   *
   */
  private transient String EAIPort = null;
  /**
   *
   * @param def String
   * @return String
   */
  public String getEAIPort(String def) {
    if ( EAIPort == null ) return def;
    return EAIPort;
  }
  /**
   *
   * @param eaiPort String
   */
  public void setEAIPort(String eaiPort) {
    EAIPort = eaiPort;
  }
  /**
   *
   */
  private transient String EnterpriseServer = null;
  /**
   *
   * @param def String
   * @return String
   */
  public String getEnterpriseServer(String def) {
    if ( EnterpriseServer == null ) return def;
    return EnterpriseServer;
  }
  /**
   *
   * @param enterpriseServer String
   */
  public void setEnterpriseServer(String enterpriseServer) {
    EnterpriseServer = enterpriseServer;
  }
  /**
   *
   * @param def String
   * @return String
   */
  public String getEAIService(String def) {
    if ( EAIService == null ) return def;
    return EAIService;
  }
  /**
   *
   * @param eaiService String
   */
  public void setEAIService(String eaiService) {
    EAIService = eaiService;
  }
  /**
   *
   */
  private transient String EAIService = null;
  /**
   *
   */
  private transient java.util.Map losableList = null;
  /**
   *
   */
  protected final transient static Hashtable GPO = new Hashtable();
  /**
   *
   */
  protected Hashtable     EnvRoot     = new Hashtable();
  /**
   *
   */
  protected Hashtable     ParamRoot   = new Hashtable();
  /**
   *
   */
  private transient JParamObject parentParamObject = null;
  /**
   *
   * @return JParamObject
   */
  public JParamObject getParentParamObject() {
    return parentParamObject;
  }
  /**
   *
   * @param key Object
   * @param value Object
   */
  public void setLosableValue(Object key,Object value) {
    if ( losableList == null ) losableList = new Hashtable();
    if ( value == null )
      losableList.remove(key);
    else
      losableList.put(key,value);
  }
  /**
   *
   * @param key Object
   * @return Object
   */
  public Object getLosableValue(Object key) {
    return getLosableValue(key,null);
  }
  /**
   *
   * @param key Object
   * @param def Object
   * @return Object
   */
  public Object getLosableValue(Object key,Object def) {
    if ( losableList == null ) return def;
    Object o= losableList.get(key);
    if ( o == null ) o = def;
    return o;
  }
  /**
   *
   * @return Hashtable
   */
  public Hashtable getParamRoot() {
    return ParamRoot;
  }
  /**
   *
   * @param ht Hashtable
   */
  public void setParamRoot(Hashtable ht) {
    ParamRoot = ht;
  }
  /**
   *
   * @return Hashtable
   */
  public Hashtable getEnvRoot() {
    return EnvRoot;
  }
  /**
   *
   * @param ht Hashtable
   */
  public void setEnvRoot(Hashtable ht) {
    EnvRoot = ht;
  }
  /**
   *
   */
  public JParamObject() {
    // 如果是中间层，则默认的
    if ( "Middle".equals(EAI.Tier) ) {
      autoConnection = false; // 如果是中间层，则默认的是不关闭连接
    }
    this.setSecurity(EAI.getSecurity());
    this.setOffline(EAI.getOffline());
  }
  /**
   *
   * @return JParamObject
   */
  public static JParamObject Create() {
    JParamObject PO = new JParamObject();
    PO.EnvRoot = (Hashtable)GPO.clone();
    // 如果是中间层，则默认的
    if ( "Middle".equals(EAI.Tier) ) {
      PO.autoConnection = false; // 如果是中间层，则默认的是不关闭连接
    }
//    PO.Security = EAI.getSecurity();
//    PO.Offline  = EAI.getOffline();
    return PO;
  }
  /**
   *
   * @param paramKey String
   * @return JParamObject
   */
  public static JParamObject Create(String paramKey) {
    JParamObject paramObject = null;
//    paramObject = (JParamObject)Lookup.getDefault().lookups(JParamObject.class,paramKey);
    // 如果是中间层，则默认的
    if ( "Middle".equals(EAI.Tier) ) {
      paramObject.autoConnection = false; // 如果是中间层，则默认的是不关闭连接
    }
    return paramObject;
  }
  /**
   *
   * @param parent JParamObject
   * @return JParamObject
   */
  static public JParamObject Create(JParamObject parent) {
    if ( parent == null ) return null;
    JParamObject PO = new JParamObject();
    PO.parentParamObject = parent;
    // 如果是中间层，则默认的
    if ( "Middle".equals(EAI.Tier) ) {
      PO.autoConnection = false; // 如果是中间层，则默认的是不关闭连接
    }
    return PO;
  }
  /**
   *
   * @param key Object
   * @param def Object
   * @return Object
   */
  static public Object getObject(Object key,Object def) {
    Object res = GPO.get(key);
    if ( res == null ) res = def;
    return res;
  }
  /**
   *
   * @param key Object
   * @param value Object
   */
  static public void setObject(Object key,Object value) {
    if ( value == null )
      GPO.remove(key);
    else
      GPO.put(key,value);
  }
  /**
   *
   * @param PO JParamObject
   */
  static public void assign(JParamObject PO) {
    GPO.putAll(PO.getEnvRoot());
  }
  /**
   *
   */
  static public void clear() {
    GPO.clear();
  }
  /**
   *
   */
  public void DeleteParam() {
    ParamRoot = null;
  }
  /**
   *
   */
  private void createParam() {
    if ( ParamRoot == null ) ParamRoot = new Hashtable();
  }
  /**
   *
   */
  private void createEnv() {
    if ( EnvRoot == null ) EnvRoot = new Hashtable();
  }
  /**
   *
   * @param ParamName String
   * @return String
   */
  public String GetValueByParamName(String ParamName) {
    return GetValueByParamName(ParamName,"");
  }
  /**
   *
   * @param ParamName String
   * @param Defalut String
   * @return String
   */
  public String GetValueByParamName(String ParamName,String Defalut) {
    String Text = null;
    if ( ParamRoot != null )
      Text = (String)ParamRoot.get(ParamName);
    if ( Text == null ) Text = Defalut;
    return Text;
  }
  /**
   *
   * @param ParamName String
   * @return int
   */
  public int GetIntByParamName(String ParamName) {
    return GetIntByParamName(ParamName,-1);
  }
  /**
   *
   * @param ParamName String
   * @param Default int
   * @return int
   */
  public int GetIntByParamName(String ParamName,int Default) {
    int Res = Default;
    try {
    Res = Integer.valueOf(GetValueByParamName(ParamName)).intValue();
    } catch ( Exception e ) {}
    return Res;
  }
  /**
   *
   * @param EnvName String
   * @return String
   */
  public String GetValueByEnvName(String EnvName) {
    return GetValueByEnvName(EnvName,"");
  }
  /**
   *
   * @param EnvName String
   * @param Default String
   * @return String
   */
  public String GetValueByEnvName(String EnvName,String Default) {
    String Text = null;
    if ( EnvRoot != null )
      Text = (String)EnvRoot.get(EnvName);
    if ( Text == null ) {
//        Text = DBConfigManager.getValue(this, EnvName, null);
//        if (Text != null)
//            return Text;
        Object obj = EAI.getEnv(EnvName, null);
        if (obj instanceof String)
            Text = (String) obj;
//        BIZMetaData bid = (BIZMetaData) EAI.getEnv(DEF_BIZMODEL, null);
//        if (bid == null)
//            bid = (BIZMetaData)this.getLosableValue(DEF_BIZMODEL);
//        if (Text == null && bid != null) {
//            String uid=unit_id;
//            if(uid==null||"".equals(uid.trim()))uid=(String)EnvRoot.get("UNIT_ID");
//            return bid.getSYS_MDL_VAL(uid, EnvName, Default);
//        }
    }
    return Text==null?Default:Text;
  }


  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public int GetIntByEnvName(String EnvName) {
    return GetIntByEnvName(EnvName,-1);
  }
  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public int GetIntByEnvName(String EnvName,int Def) {
    int Res = Def;
    try {
      Res = Integer.valueOf(GetValueByEnvName(EnvName)).intValue();
    } catch ( Exception e ) {

    }
    return Res;
  }
  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public void SetValueByParamName(String ParamName,String ParamValue) {
    this.createParam();
    if ( ParamValue == null )
      ParamRoot.remove(ParamName);
    else
      ParamRoot.put(ParamName,ParamValue);
  }
  /**
   *
   * @param key Object
   * @param value Object
   */
  public void setValue(Object key,Object value) {
    this.createParam();
    if ( value == null )
      ParamRoot.remove(key);
    else
      ParamRoot.put(key,value);
  }
  /**
   *
   * @param key Object
   * @param defvalue Object
   * @return Object
   */
  public Object getValue(Object key,Object defvalue) {
    if ( ParamRoot == null ) return defvalue;
    return ParamRoot.get(key);
  }
  /**
   *
   */
  protected String appUniqueID = null;
  /**
   *
   * @return String
   */
  public String getAppUniqueID() {
    if ( appUniqueID != null ) return appUniqueID;
    String dbid = (String)this.getEnvValue("DataBaseName",null);
    String dbno = (String)this.getEnvValue("DBNO",null);
    if ( dbid == null || dbno == null ) return null;
    return dbid+"_"+dbno;
  }
  /**
   *
   * @param id String
   */
  public void setAppUniqueID(String id) {
    appUniqueID = id;
  }
  /**
   *
   * @return Map
   */
  public java.util.Map getAttriMap() {
    return ParamRoot;
  }
  /**
   *
   * @param key Object
   * @param value Object
   */
  public void setEnvValue(Object key,Object value) {
    this.createEnv();
    if ( value == null )
      EnvRoot.remove(key);
    else
      EnvRoot.put(key,value);
  }
  /**
   *
   * @param key Object
   * @param defvalue Object
   * @return Object
   */
  public Object getEnvValue(Object key,Object defvalue) {
    Object res = EnvRoot.get(key);
    if ( EnvRoot != null ) {
      res = EnvRoot.get(key);
    }
    if ( res == null )
      res = EAI.getEnv(key,defvalue);
    return res;
  }
  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public void SetIntByParamName(String ParamName,int ParamValue) {
    SetValueByParamName(ParamName,String.valueOf(ParamValue));
  }
  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public void SetValueByEnvName(String EnvName,String EnvValue) {
    this.createEnv();
    if ( EnvValue == null )
      EnvRoot.remove(EnvName);
    else
      EnvRoot.put(EnvName,EnvValue);
  }
  //----------------------------------------------------------------------------------------------
  //����:
  //���\uFFFD: Skyline(2001.12.29)
  //ʵ��: Skyline
  //�޸�:
  //----------------------------------------------------------------------------------------------
  public void SetIntByEnvName(String ParamName,int ParamValue) {
    SetValueByEnvName(ParamName,String.valueOf(ParamValue));
  }
  /**
   *
   */
  private transient java.util.List connList = null;
  /**
   *
   * @return List
   */
  public java.util.List getConnList() {
    return connList;
  }
  /**
   *
   */
  private boolean autoConnection = true;
  /**
   *
   * @return boolean
   */
  public boolean isAutoConnection() {
    Object AUTOCLOSE = getLosableValue("AUTOCLOSE");
    return ( autoConnection && ( AUTOCLOSE == null || "1".equals(AUTOCLOSE) ) );
  }
  /**
   *
   * @param b boolean
   */
  public void setAutoConnection(boolean b) {
    if ( b && losableList != null ) losableList.remove("AUTOCLOSE");
    autoConnection = b;
  }
  /**
   *
   * @param conn Object
   */
  public void addConn(Object conn) {
    addConn(this,conn);
  }
  /**
   *
   * @param conn Object
   */
  public void removeConn(Object conn) {
    removeConn(this,conn);
  }
  /**
   *
   * @param PO JParamObject
   * @param conn Object
   */
  private static void addConn(JParamObject PO,Object conn) {
    //  如果设置自动管理连接，则可以设置数据库连接
    if ( PO.parentParamObject == null && PO.isAutoConnection() ) {
      if ( PO.connList == null ) PO.connList = new ArrayList();
      PO.connList.add(conn);
    } else {
      addConn(PO.parentParamObject,conn);
    }
  }
  /**
   *
   * @param PO JParamObject
   * @param conn Object
   */
  private static void removeConn(JParamObject PO,Object conn) {
    if ( PO.parentParamObject == null ) {
      if ( PO.connList == null ) PO.connList = new ArrayList();
      PO.connList.remove(conn);
    } else {
      removeConn(PO.parentParamObject,conn);
    }
  }
  /**
   * getBIZEDate
   *
   * @return String
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public String getBIZEDate() {
    return this.GetValueByParamName("BIZEDate",null);
  }

  /**
   * getBIZSDate
   *
   * @return String
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public String getBIZSDate() {
    return this.GetValueByParamName("BIZSDate",null);
  }

  /**
   * getBIZType
   *
   * @return String
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public String getBIZType() {
    return this.GetValueByParamName("BIZType",null);
  }

  /**
   * getBIZUnit
   *
   * @return String
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public String getBIZUnit() {
    return this.GetValueByParamName("BIZUnit",null);
  }

  /**
   * getBIZValue
   *
   * @param object Object
   * @param object1 Object
   * @return Object
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public Object getBIZValue(Object object, Object object1) {
    return this.getValue(object,object1);
  }

  /**
   * getDateType
   *
   * @return String
   * @todo Implement this com.efounder.model.biz.BIZContext method
   */
  public String getDateType() {
    return this.GetValueByParamName("DateType",null);
  }
  /**
   *
   * @param object Object
   * @param object1 Object
   */
  public void setBIZValue(Object object, Object object1) {
    this.setValue(object,object1);
  }
  /**
   *
   * @return JParamObject
   */
  public Object clone() {
    JParamObject po = JParamObject.Create();
    if ( po.EnvRoot == null ) po.EnvRoot = new Hashtable();
    po.EnvRoot.clear();po.EnvRoot.putAll(this.getEnvRoot());
    if ( po.ParamRoot == null ) po.ParamRoot = new Hashtable();
    po.ParamRoot.clear();po.ParamRoot.putAll(this.getParamRoot());
    po.appUniqueID = this.appUniqueID;
    po.autoConnection = this.autoConnection;
    po.httpServlet = this.httpServlet;
    po.httpServletRequest = this.httpServletRequest;
    po.httpServletResponse = this.httpServletResponse;
    po.eaiServer = this.eaiServer;
    po.EAIPort = this.EAIPort;
    po.EAIService = this.EAIService;
    po.EnterpriseServer = this.EnterpriseServer;
    po.serviceURL = this.serviceURL;
    return po;
  }


  /**
   *
   */
  protected java.util.List loginfoList = null;
  /**
   *
   * @param list List
   */
  public void setLoginfoList(java.util.List list) {
    if ( list != null && list.size() > 0 )
      loginfoList = list;
    else
      loginfoList = null;
  }
  /**
   *
   * @return List
   */
  public java.util.List getLoginfoList() {
    return loginfoList;
  }
  /**
   *
   */
  protected String unit_id = null;
  /**
   *
   * @return String
   */
  public String getUNIT_ID() {
    return unit_id;
  }
  /**
   *
   * @param unit_id String
   */
  public void setUNIT_ID(String unit_id) {
    this.unit_id = unit_id;
  }
}
